package com.tiancikeji.zaoke.app.ajax.food;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.tiancikeji.zaoke.app.biz.FoodBiz;
import com.tiancikeji.zaoke.app.common.dispatch.AjaxClass;
import com.tiancikeji.zaoke.app.common.dispatch.AjaxMethod;
import com.tiancikeji.zaoke.app.constants.ErrorCodeConstants;
import com.tiancikeji.zaoke.app.model.food.Food;
import com.tiancikeji.zaoke.app.model.food.FoodNutrition;
import com.tiancikeji.zaoke.app.model.food.FoodPrice;
import com.tiancikeji.zaoke.app.model.food.FoodTag;
import com.tiancikeji.zaoke.app.util.ZaokeUtils;

@AjaxClass
public class FoodAjax {
	
	private FoodBiz foodBiz;

	@AjaxMethod(path = "food/list")
	public Map<Object, Object> getFoodList(Map<String, String> params) {
		Map<Object, Object> result = new HashMap<Object, Object>();
		
		Map<String, List<Food>> foods = foodBiz.getFoodList(ZaokeUtils.getImageType(params.get(ZaokeUtils.OS_KEY)));
		
		String def = foodBiz.getDefaultFoodType();
		
		if(StringUtils.isNotBlank(def))
			result.put("default", def);
		
		genFoods4View(foods, result);
		
		result.put("status", ErrorCodeConstants.STATUS_OK);
		
		return result;
	}

	private void genFoods4View(Map<String, List<Food>> foods, Map<Object, Object> result) {
		if(result != null && foods != null && foods.size() > 0)
			for(String key : foods.keySet()){
				List<Food> tmp = foods.get(key);
				if(tmp == null || tmp.size() <= 0)
					continue;
				
				List<Map<String, Object>> foodtmp = new ArrayList<Map<String,Object>>();
				result.put(key, foodtmp);
				
				for(Food food : tmp)
					if(food != null && food.getId() > 0 && StringUtils.isNotBlank(food.getName())){
						Map<String, Object> tmp2 = new HashMap<String, Object>();
						tmp2.put("id", food.getId());
						tmp2.put("name", food.getName());
						tmp2.put("sale_price", food.getSalePrice());
						tmp2.put("desc", food.getDesc());
						
						if(food.getTags() != null && food.getTags().size() > 0){
							List<String> tags = new ArrayList<String>();
							for(FoodTag tag : food.getTags())
								tags.add(tag.getTag());
							tmp2.put("tags", tags);
						}
						
						if(food.getNutritions() != null && food.getNutritions().size() > 0){
							Map<String, String> nutritions = new HashMap<String, String>();
							for(FoodNutrition nutrition : food.getNutritions())
								if(StringUtils.isNotBlank(nutrition.getValue()) && nutrition.getNutrition() != null && StringUtils.isNotBlank(nutrition.getNutrition().getName()))
									nutritions.put(nutrition.getNutrition().getName(), nutrition.getValue());
							tmp2.put("nutrition", nutritions);
						}
						
						if(food.getImage() != null)
							tmp2.put("image_url", food.getImage().getUrl());
						
						if(food.getPromotion() != null)
							tmp2.put("promotion", "1");
						else
							tmp2.put("promotion", "0");
						
						foodtmp.add(tmp2);
					}
			}
	}

	@AjaxMethod(path = "food/info")
	public Map<Object, Object> getFoodInfo(Map<String, String> params) {
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_FOOD_NO_FOOD);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_FOOD_NO_FOOD));

		if(params != null && params.containsKey("id")){
			long foodId = ZaokeUtils.getLong(params.get("id"));
			if(foodId > 0){
				Food food = foodBiz.getFoodInfo(foodId, ZaokeUtils.getImageType(params.get(ZaokeUtils.OS_KEY)));
				
				if(food != null){
					genFood4View(food, result);
					result.put("status", ErrorCodeConstants.STATUS_OK);
					result.remove("msg");
				} else {
					result.put("status", ErrorCodeConstants.STATUS_FOOD_NOT_EXISTS);
					result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_FOOD_NOT_EXISTS));
				}
			}
		}
			
		return result;
	}

	private void genFood4View(Food food, Map<Object, Object> result) {
		if(food != null && food.getId() > 0 && StringUtils.isNotBlank(food.getName())){
			result.put("id", food.getId());
			result.put("type", food.getType());
			result.put("name", food.getName());
			result.put("desc", food.getDesc());
			result.put("sale_price", food.getSalePrice());
			
			if(food.getTags() != null && food.getTags().size() > 0){
				List<String> tags = new ArrayList<String>();
				for(FoodTag tag : food.getTags())
					tags.add(tag.getTag());
				result.put("tags", tags);
			}
			
			if(food.getNutritions() != null && food.getNutritions().size() > 0){
				Map<String, String> nutritions = new HashMap<String, String>();
				for(FoodNutrition nutrition : food.getNutritions())
					if(StringUtils.isNotBlank(nutrition.getValue()) && nutrition.getNutrition() != null && StringUtils.isNotBlank(nutrition.getNutrition().getName()))
						nutritions.put(nutrition.getNutrition().getName(), nutrition.getValue());
				result.put("nutrition", nutritions);
			}
			
			if(food.getImage() != null)
				result.put("image_url", food.getImage().getUrl());
			
			if(food.getPromotion() != null)
				result.put("promotion", "1");
			else
				result.put("promotion", "0");
		}
	}

	@AjaxMethod(path = "food/buy")
	public Map<Object, Object> getFoodPrice(Map<String, String> params) {
		Map<Object, Object> result = new HashMap<Object, Object>();
		result.put("status", ErrorCodeConstants.STATUS_FOOD_NO_FOOD);
		result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_FOOD_NO_FOOD));

		if(params != null && (params.containsKey("1") || params.containsKey("2"))){
			long foodId1 = ZaokeUtils.getLong(params.get("1"));
			long foodId2 = ZaokeUtils.getLong(params.get("2"));
			
			if(foodId1 <= 0 && foodId2 <= 0){
				return result;
			}				
			
			Food food1 = null;
			if(foodId1 > 0)
				food1 = foodBiz.getFoodInfo(foodId1, ZaokeUtils.getImageType(params.get(ZaokeUtils.OS_KEY)));
			Food food2 = null;
			if(foodId2 > 0)
				food2 = foodBiz.getFoodInfo(foodId2, ZaokeUtils.getImageType(params.get(ZaokeUtils.OS_KEY)));
			
			if(food1 == null && food2 == null){
				result.put("status", ErrorCodeConstants.STATUS_FOOD_NOT_EXISTS);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_FOOD_NOT_EXISTS));
				return result;
			}
			
			if(food1 != null && food2 != null && food1.getType() == food2.getType()){
				result.put("status", ErrorCodeConstants.STATUS_FOOD_WRONG_PAIR);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_FOOD_WRONG_PAIR));
				return result;
			}
			
			FoodPrice price = foodBiz.getFoodPrice(food1, food2);
			
			if(price != null && (price.getSalePrice() >= 0 || price.getComboPrice() > 0 || price.getPromotionPrice() > 0)){
				genPriceForView(price, result);
				result.put("status", ErrorCodeConstants.STATUS_OK);
				result.remove("msg");
			}
			else{
				result.put("status", ErrorCodeConstants.STATUS_FOOD_SALE_OUT);
				result.put("msg", ErrorCodeConstants.ERROR_MSG_MAP.get(ErrorCodeConstants.STATUS_FOOD_SALE_OUT));
			}
		}
			
		return result;
	}

	private void genPriceForView(FoodPrice price, Map<Object, Object> result) {
		if(price != null){
			if(price.getSalePrice() > 0)
				result.put("sale_price", price.getSalePrice());
			if(price.getComboPrice() > 0)
				result.put("combo_price", price.getComboPrice());
			if(price.getPromotionPrice() > 0)
				result.put("promotion_price", price.getPromotionPrice());
		}
	}

	public FoodBiz getFoodBiz() {
		return foodBiz;
	}

	public void setFoodBiz(FoodBiz foodBiz) {
		this.foodBiz = foodBiz;
	}
}
